﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GaiNo.Core.Helpers;
using GaiNo.Core.Extensions;
namespace GaiNo.Core.Services
{
    public class Message : IMessage
    {
        const string _KEY = "MESSAGE";
        const string _TYPE = "MESSAGE_TYPE";
        private LanguageType _language = LanguageType.en;
        protected string TypeKey
        {
            get
            {
                return _TYPE;
            }
        }
        protected string MessageKey
        {
            get { return _KEY; }
        }
        #region IMessage 成员

        public void Success(string message)
        {
            SetMessage(message, MessageType.Success);
        }

        public void Error(string message)
        {
            SetMessage(message, MessageType.Error);
        }

        public void Info(string message)
        {
            SetMessage(message, MessageType.Info);
        }

        public void Wran(string message)
        {
            SetMessage(message, MessageType.Wran);
        }

        public void Debug(string message)
        {
            SetMessage(message, MessageType.Debug);
        }
        private bool _isShowType = true;
        protected bool IsShowType
        {
            set
            {
                _isShowType = value;
            }
        }
        protected virtual void Clear()
        {
            SessionHelper.Remove(_TYPE);
            SessionHelper.Remove(_KEY);
        }
        public string Get()
        {
            return Get(_language);
        }
        protected virtual string Get(LanguageType language)
        {
            string result = string.Empty;
            string title = string.Empty;
            MessageType type = SessionHelper.Get<MessageType>(_TYPE);
            bool debug = false;

#if DEBUG
            debug = true;
#endif
            if (type == MessageType.Debug && !debug)
                return string.Empty;
            if (type != MessageType.None && _isShowType)
                title = type.ToString();
            string message = SessionHelper.Get<string>(_KEY);
            if (!message.IsNullOrEmpty())
                result += ApplicationContext.Current.LanguageRecource.GetResource(title + " : " + message, language);
            Clear();
            return result;
        }

        #endregion

        #region IApplicationState 成员

        public bool IsValid
        {
            get
            {
                return SessionHelper.Get<string>(_KEY).IsNullOrEmpty() && SessionHelper.Get<MessageType>(_TYPE) == MessageType.None;
            }
        }

        #endregion

        protected virtual void SetMessage(string message, MessageType type)
        {
            SessionHelper.Set(_TYPE, type);
            SessionHelper.Set(_KEY, message);
        }

        #region IMessage 成员

        public LanguageType Language
        {
            get
            {
                return _language;
            }
            set
            {
                _language = value;
            }
        }

        #endregion
    }
    public enum MessageType
    {
        None,
        Error,
        Success,
        Info,
        Wran,
        Debug
    }
}
